package main

import (
	"fmt"
	"time"
)

type Point struct {
	x int
	y int
}

type Way struct {
	leftTop, rightBottom Point
	allPoint             []Point
}

func (w *Way) getAllPoint() {
	var ret []Point
	x1, y1 := w.leftTop.x, w.leftTop.y
	x2, y2 := w.rightBottom.x, w.rightBottom.y
	for index := y2; index <= y1; index++ {
		ret = append(ret, Point{x1, index}, Point{x2, index})
	}
	for index := x1 + 1; index < x2; index++ {
		ret = append(ret, Point{index, y2}, Point{index, y1})
	}
	w.allPoint = ret
}

var myMap [30][30]bool

func (w *Way) init(leftTop, rightBottom Point) {
	w.leftTop = leftTop
	w.rightBottom = rightBottom
	w.getAllPoint()
	for _, v := range w.allPoint {
		myMap[v.y][v.x] = true
	}
}

func (w Way) check(point Point) bool {
	for _, v := range w.allPoint {
		if v == point {
			return true
		} else {
			continue
		}
	}
	return false
}

type Obj struct {
	way      Way
	position Point
	dir      bool
	speed    int
}

func (o *Obj) init(way Way) {
	o.way = way
	o.position = way.leftTop
	o.dir = true
	o.speed = 1
}

func (o *Obj) move() {
	x1, y1 := o.way.leftTop.x, o.way.leftTop.y
	x2, y2 := o.way.rightBottom.x, o.way.rightBottom.y
	curP := o.position
	for index := 0; index < o.speed; index++ {
		if o.dir {
			if curP.x == x1 && curP.y < y1 && curP.y >= y2 {
				o.position = Point{x1, o.position.y + 1}
				return
			} else if curP.x == x2 && curP.y <= y1 && curP.y > y2 {
				o.position = Point{x2, o.position.y - 1}
				return
			} else if curP.y == y1 && curP.x < x2 && curP.x >= x1 {
				o.position = Point{o.position.x + 1, y1}
				return
			} else if curP.y == y2 && curP.x <= x2 && curP.x > x1 {
				o.position = Point{o.position.x - 1, y2}
				return
			}
		} else {
			if curP.x == x1 && curP.y <= y1 && curP.y > y2 {
				o.position = Point{x1, o.position.y - 1}
				return
			} else if curP.x == x2 && curP.y < y1 && curP.y >= y2 {
				o.position = Point{x2, o.position.y + 1}
				return
			} else if curP.y == y1 && curP.x <= x2 && curP.x > x1 {
				o.position = Point{o.position.x - 1, y1}
				return
			} else if curP.y == y2 && curP.x < x2 && curP.x >= x1 {
				o.position = Point{o.position.x + 1, y2}
				return
			}
		}
	}
}

func run(arg ...*Obj) {
	for true {
		for y, v0 := range myMap {
			for x, v1 := range v0 {
				flag := false //轨道上是否有车
				for _, car := range arg {
					if car.position.x == x && car.position.y == y && !flag {
						fmt.Printf("★ ")
						flag = true
					}
				}
				if flag {
					continue
				} else if v1 {
					fmt.Printf("〇")
				} else {
					fmt.Printf("  ")
				}
			}
			fmt.Printf("\n")
		}
		for _, car := range arg {
			car.move()
		}
		time.Sleep(1000000000)
	}

}

func main() {
	var road, test Way
	var car, newCar Obj
	road.init(Point{0, 10}, Point{5, 0})
	test.init(Point{6, 10}, Point{10, 5})
	car.init(road)
	newCar.init(test)
	newCar.speed = 2
	newCar.dir = false
	run(&car,&newCar)
}
